package com.yhg.qxmall.cartorder.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yhg.qxmall.cartorder.feign.FrontWareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yhg.qxmall.cartorder.entity.FmsCartItemEntity;
import com.yhg.qxmall.cartorder.service.FmsCartItemService;
import com.yhg.common.utils.PageUtils;
import com.yhg.common.utils.R;



/**
 * 
 *
 * @author 凌月青缃
 * @email 13@qxmall.com
 * @date 2020-12-21 23:23:18
 */
@RestController
@RequestMapping("cartorder/cartitem")
public class FrontCartItemController {
    @Autowired
    private FmsCartItemService cartItemService;

    /**移除购物车
     */
    @DeleteMapping("/deleteCartItem")
//    public R deleteCartItem(@RequestParam Long memberId,@RequestParam Long skuId){
    public R deleteCartItem(@RequestParam Long memberId,@RequestParam Long skuId){
        String s = cartItemService.deleteCartItem(memberId,skuId);
        return R.ok().put("msg",s);
    }

    /**购物车结算
     * 默认它点击结算了删除购物车
     */
    @DeleteMapping("/toOrder")
//    public R deleteCartItem(@RequestParam Long memberId,@RequestParam Long skuId){
    public R toOrder(@RequestParam Long memberId,@RequestParam Long skuId,@RequestParam Integer count){
        String s = cartItemService.createFromCart(memberId,skuId,count);
        return R.ok().put("msg",s);
    }

    /**
     * 查询购物车的方法
     */
    @GetMapping("/getCartList")
    public R getCartList(@RequestParam Long memberId){
        //先更具用户id查询他所拥有的购物项
        List<FmsCartItemEntity> memberCart = cartItemService.list(new QueryWrapper<FmsCartItemEntity>().eq("member_id", memberId));
        if (memberCart!=null){
            return R.ok().put("data",memberCart);
        }else {
            return R.error().put("msg","还没有购物车哟，快去添加吧！");
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = cartItemService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		FmsCartItemEntity fmsCartItem = cartItemService.getById(id);

        return R.ok().put("fmsCartItem", fmsCartItem);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody FmsCartItemEntity fmsCartItem){
		cartItemService.save(fmsCartItem);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody FmsCartItemEntity fmsCartItem){
		cartItemService.updateById(fmsCartItem);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		cartItemService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
